package com.ruoyi.sample.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import com.ruoyi.attachedData.domain.AttachedData;
import com.ruoyi.attachedRecord.domain.AttachedRecord;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.sampleRecord.domain.SampleRecord;
import com.ruoyi.sampleRecord.mapper.SampleRecordMapper;
import com.ruoyi.sampleRecord.service.ISampleRecordService;
import com.ruoyi.workload.domain.Workload;
import com.ruoyi.workload.mapper.WorkloadMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.sample.mapper.SampleMapper;
import com.ruoyi.sample.domain.Sample;
import com.ruoyi.sample.service.ISampleService;

import javax.annotation.Resource;

/**
 * 实验样品Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-08
 */
@Service
public class SampleServiceImpl implements ISampleService
{
    @Autowired
    private SampleMapper sampleMapper;

    @Resource
    private SampleRecordMapper sampleRecordMapper;

    @Resource
    private WorkloadMapper workloadMapper;

    /**
     * 查询实验样品
     *
     * @param id 实验样品主键
     * @return 实验样品
     */
    @Override
    public Sample selectSampleById(Long id)
    {
        return sampleMapper.selectSampleById(id);
    }

    /**
     * 查询实验样品列表
     *
     * @param sample 实验样品
     * @return 实验样品
     */
    @Override
    public List<Sample> selectSampleList(Sample sample)
    {
        List<Sample> sampleDataList = sampleMapper.selectSampleList(sample);
        if(!sampleDataList.isEmpty()) {
            SampleRecord attachedRecord = new SampleRecord();
            for (Sample attachedData1 : sampleDataList) {
                Integer completionNumber = 0;
                attachedRecord.setSampleId(attachedData1.getId());
                List<SampleRecord> attachedRecordList = sampleRecordMapper.selectSampleRecordList(attachedRecord);
                if (!attachedRecordList.isEmpty()) {
                    for (SampleRecord attachedData2 : attachedRecordList) {
                        if(StringUtils.isNotEmpty(attachedData2.getSampleNumber())){
                            completionNumber += Integer.parseInt(attachedData2.getSampleNumber());
                        }
                    }
                }
                attachedData1.setCompletionProgress(String.valueOf(completionNumber));

            }
        }
        return sampleDataList;
    }

    /**
     * 新增实验样品
     *
     * @param sample 实验样品
     * @return 结果
     */
    @Override
    public int insertSample(Sample sample)
    {
        sample.setCreateTime(DateUtils.getNowDate());
        int size = sampleMapper.insertSample(sample);

        //添加工作量记录
        Workload workload = new Workload();
        BigDecimal number = new BigDecimal(sample.getNumber());
        BigDecimal checkHours = new BigDecimal(1);
        BigDecimal examineHours = new BigDecimal(1);
        BigDecimal chargeHours = new BigDecimal(1);
        BigDecimal experimentHours = new BigDecimal(1);
        BigDecimal dataHours = new BigDecimal(1);
        if("361".equals(sample.getProject())){
            BigDecimal decimal = new BigDecimal(3);
            BigDecimal check = number.divide(BigDecimal.valueOf(6), 0, RoundingMode.HALF_UP).multiply(new BigDecimal("0.1")).setScale(0,RoundingMode.HALF_UP);
            if(check.compareTo(decimal) > 0){
                checkHours = check;
            }else if (check.compareTo(decimal) < 0){
                checkHours = decimal;
            }else{
                checkHours = decimal;
            }
            BigDecimal examine = number.divide(BigDecimal.valueOf(6), 0, RoundingMode.HALF_UP).multiply(new BigDecimal("0.1")).setScale(0,RoundingMode.HALF_UP);
            if(check.compareTo(decimal) > 0){
                examineHours = examine;
            }else if (check.compareTo(decimal) < 0){
                examineHours = decimal;
            }else{
                examineHours = decimal;
            }
            BigDecimal charge = number.divide(BigDecimal.valueOf(6), 0, RoundingMode.HALF_UP).multiply(new BigDecimal("0.3")).setScale(0,RoundingMode.HALF_UP);
            if(check.compareTo(decimal) > 0){
                chargeHours = charge;
            }else if (check.compareTo(decimal) < 0){
                chargeHours = decimal;
            }else{
                chargeHours = decimal;
            }
            BigDecimal experiment = number.divide(BigDecimal.valueOf(6), 0, RoundingMode.HALF_UP).multiply(new BigDecimal("0.3")).setScale(0,RoundingMode.HALF_UP);
            if(check.compareTo(decimal) > 0){
                experimentHours = experiment;
            }else if (check.compareTo(decimal) < 0){
                experimentHours = decimal;
            }else{
                experimentHours = decimal;
            }
            BigDecimal data = number.divide(BigDecimal.valueOf(6), 0, RoundingMode.HALF_UP).multiply(new BigDecimal("0.2")).setScale(0,RoundingMode.HALF_UP);
            if(check.compareTo(decimal) > 0){
                dataHours = data;
            }else if (check.compareTo(decimal) < 0){
                dataHours = decimal;
            }else{
                dataHours = decimal;
            }
        }else if("411".equals(sample.getProject())){
            BigDecimal decimal = new BigDecimal(3);
            BigDecimal check = number.multiply(new BigDecimal("0.1")).setScale(0,RoundingMode.HALF_UP);
            if(check.compareTo(decimal) > 0){
                checkHours = check;
            }else if (check.compareTo(decimal) < 0){
                checkHours = decimal;
            }else{
                checkHours = decimal;
            }
            BigDecimal examine = number.multiply(new BigDecimal("0.1")).setScale(0,RoundingMode.HALF_UP);
            if(check.compareTo(decimal) > 0){
                examineHours = examine;
            }else if (check.compareTo(decimal) < 0){
                examineHours = decimal;
            }else{
                examineHours = decimal;
            }
            BigDecimal charge = number.multiply(new BigDecimal("0.3")).setScale(0,RoundingMode.HALF_UP);
            if(check.compareTo(decimal) > 0){
                chargeHours = charge;
            }else if (check.compareTo(decimal) < 0){
                chargeHours = decimal;
            }else{
                chargeHours = decimal;
            }
            BigDecimal experiment = number.multiply(new BigDecimal("0.3")).setScale(0,RoundingMode.HALF_UP);
            if(check.compareTo(decimal) > 0){
                experimentHours = experiment;
            }else if (check.compareTo(decimal) < 0){
                experimentHours = decimal;
            }else{
                experimentHours = decimal;
            }
            BigDecimal data = number.multiply(new BigDecimal("0.2")).setScale(0,RoundingMode.HALF_UP);
            if(check.compareTo(decimal) > 0){
                dataHours = data;
            }else if (check.compareTo(decimal) < 0){
                dataHours = decimal;
            }else{
                dataHours = decimal;
            }
        }else if("412".equals(sample.getProject())){
            BigDecimal decimal = new BigDecimal(1);
            BigDecimal check = number.divide(BigDecimal.valueOf(20), 0, RoundingMode.HALF_UP).multiply(new BigDecimal("0.1")).setScale(0,RoundingMode.HALF_UP);
            if(check.compareTo(decimal) > 0){
                checkHours = check;
            }else if (check.compareTo(decimal) < 0){
                checkHours = decimal;
            }else{
                checkHours = decimal;
            }
            BigDecimal examine = number.divide(BigDecimal.valueOf(20), 0, RoundingMode.HALF_UP).multiply(new BigDecimal("0.1")).setScale(0,RoundingMode.HALF_UP);
            if(check.compareTo(decimal) > 0){
                examineHours = examine;
            }else if (check.compareTo(decimal) < 0){
                examineHours = decimal;
            }else{
                examineHours = decimal;
            }
            BigDecimal charge = number.divide(BigDecimal.valueOf(20), 0, RoundingMode.HALF_UP).multiply(new BigDecimal("0.3")).setScale(0,RoundingMode.HALF_UP);
            if(check.compareTo(decimal) > 0){
                chargeHours = charge;
            }else if (check.compareTo(decimal) < 0){
                chargeHours = decimal;
            }else{
                chargeHours = decimal;
            }
            BigDecimal experiment = number.divide(BigDecimal.valueOf(20), 0, RoundingMode.HALF_UP).multiply(new BigDecimal("0.3")).setScale(0,RoundingMode.HALF_UP);
            if(check.compareTo(decimal) > 0){
                experimentHours = experiment;
            }else if (check.compareTo(decimal) < 0){
                experimentHours = decimal;
            }else{
                experimentHours = decimal;
            }
            BigDecimal data = number.divide(BigDecimal.valueOf(20), 0, RoundingMode.HALF_UP).multiply(new BigDecimal("0.2")).setScale(0,RoundingMode.HALF_UP);
            if(check.compareTo(decimal) > 0){
                dataHours = data;
            }else if (check.compareTo(decimal) < 0){
                dataHours = decimal;
            }else{
                dataHours = decimal;
            }
        }else if("372".equals(sample.getProject())){
            BigDecimal decimal = new BigDecimal(1);
            BigDecimal check = number.divide(BigDecimal.valueOf(10), 0, RoundingMode.HALF_UP).multiply(new BigDecimal("0.1")).setScale(0,RoundingMode.HALF_UP);
            if(check.compareTo(decimal) > 0){
                checkHours = check;
            }else if (check.compareTo(decimal) < 0){
                checkHours = decimal;
            }else{
                checkHours = decimal;
            }
            BigDecimal examine = number.divide(BigDecimal.valueOf(10), 0, RoundingMode.HALF_UP).multiply(new BigDecimal("0.1")).setScale(0,RoundingMode.HALF_UP);
            if(check.compareTo(decimal) > 0){
                examineHours = examine;
            }else if (check.compareTo(decimal) < 0){
                examineHours = decimal;
            }else{
                examineHours = decimal;
            }
            BigDecimal charge = number.divide(BigDecimal.valueOf(10), 0, RoundingMode.HALF_UP).multiply(new BigDecimal("0.3")).setScale(0,RoundingMode.HALF_UP);
            if(check.compareTo(decimal) > 0){
                chargeHours = charge;
            }else if (check.compareTo(decimal) < 0){
                chargeHours = decimal;
            }else{
                chargeHours = decimal;
            }
            BigDecimal experiment = number.divide(BigDecimal.valueOf(10), 0, RoundingMode.HALF_UP).multiply(new BigDecimal("0.3")).setScale(0,RoundingMode.HALF_UP);
            if(check.compareTo(decimal) > 0){
                experimentHours = experiment;
            }else if (check.compareTo(decimal) < 0){
                experimentHours = decimal;
            }else{
                experimentHours = decimal;
            }
            BigDecimal data = number.divide(BigDecimal.valueOf(10), 0, RoundingMode.HALF_UP).multiply(new BigDecimal("0.2")).setScale(0,RoundingMode.HALF_UP);
            if(check.compareTo(decimal) > 0){
                dataHours = data;
            }else if (check.compareTo(decimal) < 0){
                dataHours = decimal;
            }else{
                dataHours = decimal;
            }
        }else if("370".equals(sample.getProject())){
            BigDecimal decimal = new BigDecimal(1);
            BigDecimal check = number.divide(BigDecimal.valueOf(3), 0, RoundingMode.HALF_UP).multiply(new BigDecimal("0.1")).setScale(0,RoundingMode.HALF_UP);
            if(check.compareTo(decimal) > 0){
                checkHours = check;
            }else if (check.compareTo(decimal) < 0){
                checkHours = decimal;
            }else{
                checkHours = decimal;
            }
            BigDecimal examine = number.divide(BigDecimal.valueOf(3), 0, RoundingMode.HALF_UP).multiply(new BigDecimal("0.1")).setScale(0,RoundingMode.HALF_UP);
            if(check.compareTo(decimal) > 0){
                examineHours = examine;
            }else if (check.compareTo(decimal) < 0){
                examineHours = decimal;
            }else{
                examineHours = decimal;
            }
            BigDecimal charge = number.divide(BigDecimal.valueOf(3), 0, RoundingMode.HALF_UP).multiply(new BigDecimal("0.3")).setScale(0,RoundingMode.HALF_UP);
            if(check.compareTo(decimal) > 0){
                chargeHours = charge;
            }else if (check.compareTo(decimal) < 0){
                chargeHours = decimal;
            }else{
                chargeHours = decimal;
            }
            BigDecimal experiment = number.divide(BigDecimal.valueOf(3), 0, RoundingMode.HALF_UP).multiply(new BigDecimal("0.3")).setScale(0,RoundingMode.HALF_UP);
            if(check.compareTo(decimal) > 0){
                experimentHours = experiment;
            }else if (check.compareTo(decimal) < 0){
                experimentHours = decimal;
            }else{
                experimentHours = decimal;
            }
            BigDecimal data = number.divide(BigDecimal.valueOf(3), 0, RoundingMode.HALF_UP).multiply(new BigDecimal("0.2")).setScale(0,RoundingMode.HALF_UP);
            if(check.compareTo(decimal) > 0){
                dataHours = data;
            }else if (check.compareTo(decimal) < 0){
                dataHours = decimal;
            }else{
                dataHours = decimal;
            }
        }else if("371".equals(sample.getProject())){
            BigDecimal decimal = new BigDecimal(2);
            BigDecimal check = number.divide(BigDecimal.valueOf(3), 0, RoundingMode.HALF_UP).multiply(new BigDecimal("0.1")).setScale(0,RoundingMode.HALF_UP);
            if(check.compareTo(decimal) > 0){
                checkHours = check;
            }else if (check.compareTo(decimal) < 0){
                checkHours = decimal;
            }else{
                checkHours = decimal;
            }
            BigDecimal examine = number.divide(BigDecimal.valueOf(3), 0, RoundingMode.HALF_UP).multiply(new BigDecimal("0.1")).setScale(0,RoundingMode.HALF_UP);
            if(check.compareTo(decimal) > 0){
                examineHours = examine;
            }else if (check.compareTo(decimal) < 0){
                examineHours = decimal;
            }else{
                examineHours = decimal;
            }
            BigDecimal charge = number.divide(BigDecimal.valueOf(3), 0, RoundingMode.HALF_UP).multiply(new BigDecimal("0.3")).setScale(0,RoundingMode.HALF_UP);
            if(check.compareTo(decimal) > 0){
                chargeHours = charge;
            }else if (check.compareTo(decimal) < 0){
                chargeHours = decimal;
            }else{
                chargeHours = decimal;
            }
            BigDecimal experiment = number.divide(BigDecimal.valueOf(3), 0, RoundingMode.HALF_UP).multiply(new BigDecimal("0.3")).setScale(0,RoundingMode.HALF_UP);
            if(check.compareTo(decimal) > 0){
                experimentHours = experiment;
            }else if (check.compareTo(decimal) < 0){
                experimentHours = decimal;
            }else{
                experimentHours = decimal;
            }
            BigDecimal data = number.divide(BigDecimal.valueOf(3), 0, RoundingMode.HALF_UP).multiply(new BigDecimal("0.2")).setScale(0,RoundingMode.HALF_UP);
            if(check.compareTo(decimal) > 0){
                dataHours = data;
            }else if (check.compareTo(decimal) < 0){
                dataHours = decimal;
            }else{
                dataHours = decimal;
            }
        }
        workload.setChargeHours(String.valueOf(chargeHours));
        workload.setExamineHours(String.valueOf(examineHours));
        workload.setDataHours(String.valueOf(dataHours));
        workload.setCheckHours(String.valueOf(checkHours));
        workload.setExperimentHours(String.valueOf(experimentHours));
        workload.setChargePerson(Long.valueOf(sample.getChargePerson()));
        workload.setCheckPerson(Long.valueOf(sample.getCheckPerson()));
        workload.setExaminePerson(Long.valueOf(sample.getExaminePerson()));
        workload.setDataPerson(Long.valueOf(sample.getDataPerson()));
        workload.setExperimentPerson(Long.valueOf(sample.getExperimentPerson()));
        workload.setEngineeringId(sample.getEngineeringId());
        workload.setSampleId(sample.getId());
        workloadMapper.insertWorkload(workload);
        return size;
    }

    /**
     * 修改实验样品
     *
     * @param sample 实验样品
     * @return 结果
     */
    @Override
    public int updateSample(Sample sample)
    {
        sample.setUpdateTime(DateUtils.getNowDate());
        return sampleMapper.updateSample(sample);
    }


    @Override
    public int updateSampleForStatus(Long commissionId)
    {
        return sampleMapper.updateSampleForStatus(commissionId);
    }

    /**
     * 批量删除实验样品
     *
     * @param ids 需要删除的实验样品主键
     * @return 结果
     */
    @Override
    public int deleteSampleByIds(Long[] ids)
    {
        return sampleMapper.deleteSampleByIds(ids);
    }


    /**
     * 批量逻辑删除实验样品
     *
     * @param ids 需要删除的实验样品主键
     * @return 结果
     */
    @Override
    public int logicDeleteSample(Long[] ids,String delType,Long delUser)
    {
        return sampleMapper.logicDeleteSample(ids,delType,delUser);
    }

    /**
     * 删除实验样品信息
     *
     * @param id 实验样品主键
     * @return 结果
     */
    @Override
    public int deleteSampleById(Long id)
    {
        return sampleMapper.deleteSampleById(id);
    }
}
